<?php
class attributeModel extends Model
{
    /**
     * 生成spid 
     * 
     * @param int $pid 父级ID
     */
    public function get_spid($pid) {
        if (!$pid) {
            return 0; 
        }
        $pspid = $this->where(array('id'=>$pid))->getField('spid');
        if ($pspid) {
            $spid = $pspid . $pid . '|';
        } else {
            $spid = $pid . '|';
        }
        return $spid;
    }
    
    /**
     * 获取分类下面的所有子分类的ID集合
     * 
     * @param int $id
     * @param bool $with_self
     * @return array $array 
     */
    public function get_child_ids($id, $with_self=false) {
        $spid = $this->where(array('id'=>$id))->getField('spid');
        $spid = $spid ? $spid .= $id .'|' : $id .'|';
        $id_arr = $this->field('id')->where(array('spid'=>array('like', $spid.'%')))->select();
        $array = array();
        foreach ($id_arr as $val) {
            $array[] = $val['id'];
        }
        $with_self && $array[] = $id;
        return $array;
    }
    
    /**
     * 检测分类是否存在
     * 
     * @param string $name
     * @param int $pid
     * @param int $id
     * @return bool 
     */
    public function name_exists($name, $pid, $id=0) {
        $where = "name='" . $name . "' AND pid='" . $pid . "' AND id<>'" . $id . "'";
        $result = $this->where($where)->count('id');
        if ($result) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 写入缓存
     */
    public function cate_cache() {
        $attribute_list = array();
        $cate_data = $this->where('status=1')->order('ordid')->select();
        foreach ($cate_data as $val) {
            $attribute_list['s'][$val['pid']][$val['id']] = $val;
            $attribute_list['l'][$val['id']] = $val;
        }
        foreach($attribute_list['s'] as $k=>$v){
            if($k!=0){
                $attribute_list['t'][$attribute_list['l'][$k]['fileName']] = $v;
            }
        }
        F('attribute_list', $attribute_list);
        return $attribute_list;
    }

    /**
     * 更新则删除缓存
     */
    protected function _before_write(&$data) {
        F('attribute_list', NULL);
    }

    /**
     * 删除也删除缓存
     */
    protected function _after_delete($data, $options) {
        F('attribute_list', NULL);
    }
    /**
     * 取字段列表
     * 传入 模块名称
     * 
     */     
    public function getAttribute($type){        
       $list = $this->cate_cache();
       $returnArr = array();
       //print_r($list);
       foreach($list['s'][0] as $k=>$v){
            // 找到
            if($v['modular']==$type){
               $returnArr = $list['s'][$v['id']]+$returnArr;
            }
       }
       foreach($returnArr as $rk=>&$rv){
            $rv['rules'] = json_decode($rv['rules'],1);
            $s = $rv['rules']['choices'];
            if(!empty($s)){
                $res = array();
                foreach(preg_split("/[\r\n]+/", $s) as $r) {
                  list($k, $txt) = explode('=', $r);
                  $k = trim($k);
                  $txt = trim($txt);
                  $p =& $res;
                  foreach(explode('.', $k) as $v) {
                    if(! isset($p[$v])) $p[$v] = array('txt' => $txt, 'value'=>$k, 'child' => array());
                    $p =& $p[$v]['child'];
                  }
                }
            }
            $rv['rules']['choices'] = $res;
       }
       return $returnArr;
    }
    
    
    
    /**
     * 取值 
     */ 
     
    public function getAttributeValue($type,$pids){
        $attrilist = D('attribute')->getAttribute($type);
        $ids = '';
        foreach($attrilist as $k=>$v){
            $ids .=$v['id'].',';
        }
        $ids = trim($ids,',');

        $map = array('modular'=>$type,'optionid'=>array('in',$ids));
        if(!empty($pids)){
            $pids = trim($pids,',');
           $map['pid'] = array('in',$pids); 
        }
        $list = $this->table('gd_attributevar')->where($map)->select();
        $return = array();
        //解析value
        foreach($list as $k=>$v){
            $return[$v['pid']][$v['optionid']]['name'] = $attrilist[$v['optionid']]['name'];
            $value = '';
            switch($attrilist[$v['optionid']]['type']){
                case 'select':
                     $arr = explode('.',$v['value']);
                     $temp = $attrilist[$v['optionid']]['rules']['choices'];
                     foreach($arr as $ak=>$av){
                        $value .= $temp[$av]['txt'].'-';
                        $temp   = $temp[$av]['child'];
                     }  
                     $value = trim($value,'-');
                     break;
                     
                case "radio":  
                case "checkbox":  
                    $arr = json_decode($v['value'],1);
                    foreach($arr as $ak=>$av){
                        $value .=$attrilist[$v['optionid']]['rules']['choices'][$av]['txt'].',';
                    }
                    $value = trim($value,',');
                    break;
                default:
                    $value = $v['value'];
                    break;    
                
            }
            $return[$v['pid']][$v['optionid']]['value'] = $value;
            
        }
        return $return; 
        
    }

    
}